package com.example.jbmsuppliers.floatwindow;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.doorjurisd.DoorOpenBlue;

import java.lang.reflect.Field;


public class FloatWindowSmallView extends LinearLayout  {

    public static FloatWindowSmallView floatwi;
    private final Context context;
    private final View view;
    private final TextView percentView;
    public static final int FLOATWINSOW = 0001;
    private final int FLOATIMAGE = 0002;
    public static final int FAILEDERR=0003;
    /**
     * 记录小悬浮窗的宽度
     */
    public static int viewWidth;

    /**
     * 记录小悬浮窗的高度
     */
    public static int viewHeight;

    /**
     * 记录系统状态栏的高度
     */
    private static int statusBarHeight;
    private final DoorOpenBlue doorOpenBlue;
    /**
     * 用于更新小悬浮窗的位置
     */
    private WindowManager windowManager;

    /**
     * 小悬浮窗的参数
     */
    private WindowManager.LayoutParams mParams;

    /**
     * 记录当前手指位置在屏幕上的横坐标值
     */
    private float xInScreen;

    /**
     * 记录当前手指位置在屏幕上的纵坐标值
     */
    private float yInScreen;

    /**
     * 记录手指按下时在屏幕上的横坐标的值
     */
    private float xDownInScreen;

    /**
     * 记录手指按下时在屏幕上的纵坐标的值
     */
    private float yDownInScreen;

    /**
     * 记录手指按下时在小悬浮窗的View上的横坐标的值
     */
    private float xInView;

    /**
     * 记录手指按下时在小悬浮窗的View上的纵坐标的值
     */

    private float yInView;
    private Vibrator vibratorr;
    private SharedPreferences sharedPreferences;
    public FloatWindowSmallView(final Context context) {
        super(context);
        this.context = context;
        floatwi = this;
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        LayoutInflater.from(context).inflate(R.layout.float_window_small, this);
        view = findViewById(R.id.small_window_layout);
        viewWidth = view.getLayoutParams().width;
        viewHeight = view.getLayoutParams().height;
        percentView = (TextView) findViewById(R.id.percent);
        doorOpenBlue=new DoorOpenBlue(context,null);

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度
                xInView = event.getX();
                yInView = event.getY();
                xDownInScreen = event.getRawX();
                yDownInScreen = event.getRawY() - getStatusBarHeight();
                xInScreen = event.getRawX();
                yInScreen = event.getRawY() - getStatusBarHeight();
                break;
            case MotionEvent.ACTION_MOVE:
                xInScreen = event.getRawX();
                yInScreen = event.getRawY() - getStatusBarHeight();
                // 手指移动的时候更新小悬浮窗的位置
                updateViewPosition();
                break;
            case MotionEvent.ACTION_UP:
                // 如果手指离开屏幕时，xDownInScreen和xInScreen相等，且yDownInScreen和yInScreen相等，则视为触发了单击事件。
                if (xDownInScreen == xInScreen && yDownInScreen == yInScreen) {
//                    sharedPreferences = getContext().getSharedPreferences("open_key_judge", Activity.MODE_PRIVATE);
//                    int open = sharedPreferences.getInt("intopen", 0);
//                    if (open != 1) {
//                        doorOpenBlue.OpenDoorGather("FloatWindow", percentView, getContext(),null, null);
//                    }

                }
            default:
                break;
        }
        return true;
    }

    /**
     * 将小悬浮窗的参数传入，用于更新小悬浮窗的位置。
     *
     * @param params 小悬浮窗的参数
     */
    public void setParams(WindowManager.LayoutParams params) {
        mParams = params;
    }

    /**
     * 更新小悬浮窗在屏幕中的位置。
     */
    private void updateViewPosition() {
        mParams.x = (int) (xInScreen - xInView);
        mParams.y = (int) (yInScreen - yInView);
        windowManager.updateViewLayout(this, mParams);
    }


    /**
     * 用于获取状态栏的高度。
     *
     * @return 返回状态栏高度的像素值。
     */
    private int getStatusBarHeight() {
        if (statusBarHeight == 0) {
            try {
                Class<?> c = Class.forName("com.android.internal.R$dimen");
                Object o = c.newInstance();
                Field field = c.getField("status_bar_height");
                int x = (Integer) field.get(o);
                statusBarHeight = getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return statusBarHeight;
    }


//    Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
////                case FLOATWINSOW:{
////                    String st=(String )msg.obj;
////                    MyToast.show(context,st,0);
////                    percentView.setBackground(getResources().getDrawable(R.mipmap.kaisuo_hui));
////                    handler.sendEmptyMessageDelayed(FLOATIMAGE, 10000);
////                    break;}
////                case FAILEDERR:{
////                    String sb=(String )msg.obj;
////                    MyToast.show(context,sb,0);
////                    percentView.setBackground(getResources().getDrawable(R.mipmap.lz_kaimenshibai));
////                    handler.sendEmptyMessageDelayed(FLOATIMAGE, 10000);
////                    break;
////                }
//                case FLOATIMAGE: {
//                    percentView.setBackground(getResources().getDrawable(R.mipmap.kaisuo_moren));
//                    getContext().getSharedPreferences("open_key_judge", Activity.MODE_PRIVATE).edit().clear().commit();
//                }
//
//
//            }
//        }
//
//    };


}
